package corejava.innerclass;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

/**
 * This program demonstrates anonymous inner classes.
 * @version 1.10 2004-02-27
 * @author Cay Horstmann
 */
public class AnonymousInnerClassTest {
	
	public static void main(String[] args) {
		
		TalkingClock3 clock = new TalkingClock3();
		
		clock.start(1000, true);

		// keep program running until user selects "Ok"
		JOptionPane.showMessageDialog(null, "Quit program?");
		System.exit(0);
	}
}

/**
 * A clock that prints the time in regular intervals.
 */
class TalkingClock3 {
	/**
	 * Starts the clock.
	 * @param interval the interval between messages (in milliseconds)
	 * @param beep true if the clock should beep
	 */
	public void start(int interval, final boolean beep) {
		
		//匿名内部类，实现ActionListener接口
		ActionListener listener = new ActionListener() {
			
			public void actionPerformed(ActionEvent event) {
				Date now = new Date();
				System.out.println("At the tone, the time is " + now);
				if (beep)
					Toolkit.getDefaultToolkit().beep();
			}
		};
		
		//定时器，间隔interval milliseconds 去执行ActionListener中的actionperformed()方法
		Timer t = new Timer(interval, listener);
		
		//启动定时器
		t.start();
	}
}
